package com.vr.xuecheng.learning.service.impl;

import com.vr.xuecheng.base.exception.XueChengPlusException;
import com.vr.xuecheng.common.util.SecurityUtils;
import com.vr.xuecheng.feign.client.ContentClient;
import com.vr.xuecheng.feign.client.CoursePublishClient;
import com.vr.xuecheng.feign.client.MediaClient;
import com.vr.xuecheng.feign.dto.CoursePublishDTO;
import com.vr.xuecheng.feign.dto.TeachPlanOfFeignDTO;
import com.vr.xuecheng.feign.response.Result;
import com.vr.xuecheng.learning.dto.SaveOrUpdateLearnRecordDTO;
import com.vr.xuecheng.learning.entity.CourseTables;
import com.vr.xuecheng.learning.service.ICourseTablesService;
import com.vr.xuecheng.learning.service.ILearnRecordService;
import com.vr.xuecheng.learning.service.ILearningOpenVideoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author hzh
 * @date 2025/04/11
 */
@Log4j2
@Service
@RequiredArgsConstructor
@Transactional
public class LearningOpenVideoServiceImpl implements ILearningOpenVideoService {
    private final CoursePublishClient coursePublishClient;
    private final MediaClient mediaClient;
    private final ContentClient contentClient;
    private final ICourseTablesService courseTablesService;
    private final ILearnRecordService learnRecordService;

    @Override
    public String getVideo(Long courseId, Long teachplanId, String mediaId) {
        // 在发布表里获取课程信息
        CoursePublishDTO coursePublishInfo = coursePublishClient.getCoursePublishInfoById(courseId);
        if (coursePublishInfo == null) {
            XueChengPlusException.cast("该课程未发布，无法观看");
        }
        // 远程调用内容服务，获取教学关联的媒资信息
        TeachPlanOfFeignDTO teachPlanOfFeignDTO = contentClient.getTeachPlanById(teachplanId);
        if (teachPlanOfFeignDTO==null) {
            return "";
        }
        Result<String> remoteMediaResult = mediaClient.getFilePathById(mediaId);
        if (!Result.isSuccess(remoteMediaResult)) {
            return "";
        }
        String videoUrl = remoteMediaResult.getResult();
        // 远程调用媒资管理服务，获取视频地址
        // 判断课程是否收费
        if ("201000".equals(coursePublishInfo.getCharge())) {
            // 免费
            // 直接可以观看 返回视频地址
            return videoUrl;
        }
        // 收费
        // 判断当前用户的课程表里是否有该课程
        SecurityUtils.User user = SecurityUtils.getUser();
        if (user == null) {
            XueChengPlusException.cast("未登录,无法观看收费课程");
        }
        String userId = user.getId();
        CourseTables courseTables = courseTablesService.lambdaQuery()
                .eq(CourseTables::getUserId, user.getId())
                .eq(CourseTables::getCourseId, courseId)
                .eq(CourseTables::getCourseType, "700002").one();
        if (courseTables == null) {
            XueChengPlusException.cast("未购买,无法观看收费课程");
        }
        if (courseTables.getValidtimeEnd().isBefore(LocalDateTime.now())) {
            XueChengPlusException.cast("课程已过期,无法观看,像观看的话请续期");
        }
        // 保存学习记录
        // 构建保存或者更新所需的参数
        SaveOrUpdateLearnRecordDTO saveOrUpdateLearnRecordDTO = SaveOrUpdateLearnRecordDTO.builder()
                .courseId(courseId)
                .teachplanId(teachplanId)
                .userId(userId)
                .courseName(coursePublishInfo.getName())
                .teachplanName(teachPlanOfFeignDTO.getPname())
                .build();
        learnRecordService.updateOrSaveLearnRecord(saveOrUpdateLearnRecordDTO);
        return videoUrl;
    }
}
